package pl.edu.agh.uddiProxy;

import java.util.Arrays;
import java.util.List;

import pl.edu.agh.uddiProxy.dao.DAOHelper;
import pl.edu.agh.uddiProxy.types.Factory;
import pl.edu.agh.uddiProxy.types.ParameterValue;
import pl.edu.agh.uddiProxy.types.ServiceDynamicParameters;
import pl.edu.agh.uddiProxy.types.TModel;
import dk.itst.uddi.client.query.FindBindingResult;
import dk.itst.uddi.client.query.FindBusinessResult;
import dk.itst.uddi.client.query.FindTModelResult;
import dk.itst.uddi.client.types.core.BindingTemplate;
import dk.itst.uddi.client.types.core.BusinessInfo;
import dk.itst.uddi.client.types.core.Description;
import dk.itst.uddi.client.types.core.ServiceInfo;
import dk.itst.uddi.client.types.core.TModelInfo;
import dk.itst.uddi.client.types.core.TModelInstanceInfo;

public class App {
	public static void main(String[] args) {
//		ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
//				new String[] { "context.xml" });
//		ParametersMap parametersMap = (ParametersMap) context.getBean("ParametersMap");
//		parametersMap.update();
		
		TModel model = Factory.newInstance();
		model.getModel().addNewName().setStringValue("testTmodel");
		ServiceDynamicParameters dynamicParameter = new ServiceDynamicParameters();
		ParameterValue value1 = new ParameterValue();
		value1.setName("name1");
		value1.setValue("13");
		dynamicParameter.getMap().put("name1", value1);
		ParameterValue value2 = new ParameterValue();
		value2.setName("name2");
		value2.setValue("43");
		dynamicParameter.getMap().put("name2", value2);
		model.setDynamicParameter(dynamicParameter);
		model = DAOHelper.getUDDIClient().getPublication().saveTModel(model).execute();
		
		List<String> list = DAOHelper.getDynamicParameterDAO().getTModels("p1.name = \"availability\"");
		for (Object s : list) {
			System.out.println(s);
		}
		
		
		UDDIClient client = DAOHelper.getUDDIClient();
		InquiryClient inquiryClient = client.getInquiryClient();
		System.out.println("--------------------");
		FindBusinessResult findBusinessResult = inquiryClient.findBusiness().addName("test bussines").execute();
		for (BusinessInfo businessInfo : findBusinessResult.getBusinessInfos()) {
			if (businessInfo.getServiceInfos() == null) {
				continue;
			}
			for (ServiceInfo serviceInfo : businessInfo.getServiceInfos().getServiceInfoArray()) {
				System.out.println("getServiceKey: " + serviceInfo.getServiceKey());
				
				FindBindingResult findBindingResult = inquiryClient.findBinding().setServiceKey(serviceInfo.getServiceKey()).execute();
				for (BindingTemplate bindingTemplate : findBindingResult.getTemplates()) {
					if (bindingTemplate.getTModelInstanceDetails() == null) {
						continue;
					}
					System.out.println("getBindingKey:" + bindingTemplate.getBindingKey());
					for (TModelInstanceInfo modelInstanceInfo : bindingTemplate.getTModelInstanceDetails().getTModelInstanceInfoArray()) {
						System.out.println("getTModelKey:" + modelInstanceInfo.getTModelKey());
						
						
						
//						if (modelInstanceInfo.getInstanceDetails() == null) {
//							continue;
//						}
						for (Description description : modelInstanceInfo.getDescriptionArray()) {
							System.out.println(description);							
						}
					}
				}
			}
		}
		
		FindTModelResult findTModelResult = null;
		TModelInfo[] infos = inquiryClient.findTModel().setName("myTModel").execute();
		for (TModelInfo info : infos) {
			TModel model2 = inquiryClient.getTModel(info.getTModelKey());
			System.out.println(Arrays.toString(model2.getModel().getOverviewDocArray()));
		}

		
		
//		TModel model = Factory.newInstance();
//		model.getModel().addNewName().setStringValue("testTmodel");
//		ServiceDynamicParameters dynamicParameter = new ServiceDynamicParameters();
//		ParameterValue
//		dynamicParameter.getMap().put("type1", "value1");
//		dynamicParameter.getMap().put("type2", "value2");
//		model.setDynamicParameter(dynamicParameter);
//		model = client.getPublication().saveTModel(model).execute();
//		
//		dynamicParameter = new ServiceDynamicParameters();
//		dynamicParameter.getMap().put("type1", "value2");
//		dynamicParameter.getMap().put("type2", "value1");
//		dynamicParameter.getMap().put("type3", "value3");
//		model.setDynamicParameter(dynamicParameter);
//		UpdateClient updateClient = client.getUpdate();
//		updateClient.updateTModel(model).execute();
		

	}
}
